use dta/SMDLevel.dta, clear

gen SECOND=0
replace SECOND=1 if votes2_TOTAL!=0  

*********************** party running **********************************************
preserve
foreach party in s v_a v a l t kir h_fv h fv oth nsa nkp {
gen runF_`party'=run1_`party'
replace runF_`party'=run2_`party' if SECOND==1
}
keep year SMD run*
sort year SMD
save dta/SMDLevel_Run, replace
restore


*********************** CREATE BLOCS **********************************************
foreach round in 1 2 {
gen vs`round'_LAB=vs`round'_s+vs`round'_nsa+vs`round'_nkp 
gen vs`round'_LIB=vs`round'_a+vs`round'_v_a+vs`round'_v 
gen vs`round'_CON=vs`round'_fv+vs`round'_h_fv+vs`round'_h 
gen vs`round'_OTH=1-vs`round'_LAB-vs`round'_LIB-vs`round'_CON
}

foreach bloc in LAB LIB CON OTH {
gen vsF_`bloc'=vs1_`bloc'
replace vsF_`bloc'=vs2_`bloc' if SECOND==1
}

*** generating leads and lags
xtset SMD year

foreach round in 1 2 F {
	foreach bloc in LAB LIB CON OTH {
	gen next_vs`round'_`bloc'=f3.vs`round'_`bloc'
	gen prev_vs`round'_`bloc'=l3.vs`round'_`bloc'
	}
}

keep year SMD *vs*LAB* *vs*LIB* *vs*CON* *vs*OTH* PR 
order year SMD vs1* vs2* vsF* next_vs1* next_vs2* next_vsF* prev_vs1* prev_vs2* prev_vsF* PR



********************************************************
reshape long vs1_ vs2_ vsF_ next_vs1_ next_vs2_ next_vsF_ prev_vs1_ prev_vs2_ prev_vsF_, i(SMD year) string
rename _j bloc
order year SMD 
********************************************************
gen Reform=0
replace Reform=1 if year>1920


*********************** merge: party running **********************************************
sort year SMD
merge year SMD using dta/SMDLevel_Run
assert _merge==3
drop _merge


***************************************************************************
***************** all three blocs running in all election years************
***************************************************************************

foreach round in 1 2 F{
gen run`round'_LAB=run`round'_s+run`round'_nsa+run`round'_nkp>0
gen run`round'_LIB=run`round'_a+run`round'_v_a+run`round'_v >0
gen run`round'_CON=run`round'_fv+run`round'_h_fv+run`round'_h >0
}

foreach round in 1 2 F{
gen all`round'=0
replace all`round'=1 if (run`round'_LAB+run`round'_LIB+run`round'_CON==3)
}

bysort year: tab1 all*
egen sum_all1=sum(all1),by(SMD bloc)
egen sum_allF=sum(allF),by(SMD bloc)

keep if sum_all1==7 

***************************************************************************
********************** CREATING RANK FOR TOP-TWO **************************
***************************************************************************
egen rank1=rank(vs1_),by(SMD year)
*egen rankF=rank(vsF_),by(SMD year)
*egen NoPF=rowtotal(runF_s-runF_nkp)

gen toptwo=0
replace toptwo=1 if rank1==3|rank1==4
egen test=sum(toptwo),by(SMD year)
assert test==2
replace toptwo=. if year>1920

***************************************************************************
***************************************************************************

foreach year in 1909 1912 1915 1918 1921 1924 1927 {
gen Reform`year'=0
replace Reform`year'=1 if year==`year'
}

foreach year in 1909 1912 1915 1918{
gen zz`year'=toptwo
replace zz`year'=0 if year!=`year'
egen top`year'=max(zz`year'), by(SMD bloc)
gen Reform1921Xtop`year'=Reform1921*top`year'
gen Reform1918Xtop`year'=Reform1918*top`year'
gen Reform1915Xtop`year'=Reform1915*top`year'
gen Reform1912Xtop`year'=Reform1912*top`year'
}

*********************************************
*********************************************
*********************************************

foreach year in 1918{
*foreach year in 1909 1912 1915 1918{
preserve
collapse vs1_ , by(bloc top`year' year)  

	foreach bloc in LAB LIB CON {
	twoway (scatter vs1_ year if bloc=="`bloc'" & top`year'==1, connect(l) lpattern(dash) msymbol(circle) mcolor(white) mlcolor(black)) (scatter vs1_ year if bloc=="`bloc'" & top`year'==0, connect(l) msymbol(circle) mcolor(black)) ///
	, xline(1919.5, lpattern(dash)) xtitle("") ytitle("") title(`bloc') plotregion(lcolor(white) ilcolor(white)) graphregion(color(white)) scheme(s1mono)  yscale(range(0 0.65)) ylabel(0[0.1]0.6) legend(cols(1) rows(2) bmargin(0 10 20 0) order(1 "Toptwo `year' = 1" 2 "Toptwo `year' = 0"))
	graph save figures/gph/_`bloc'.gph, replace
	}

cd figures/gph
grc1leg _LAB.gph _LIB.gph _CON.gph , plotregion(lcolor(white) ilcolor(white)) graphregion(color(white)) scheme(s1mono) row(2) legendfrom(_LAB.gph) position(5) ring(0)
graph export ../FigureA9.pdf, replace
cd ../../
restore
}
